package thread.t2;

class Producer implements Runnable {
	P q = null;

	public Producer(P q) {
		this.q = q;
	}

	public void run() {
		int i = 0;
		while (true) {
			if (i == 0) {
				q.set("张三", "男");
			} else {
				q.set("李四", "女");
			}
			i = (i + 1) % 2;
		}
	}
}

class P {
	private String name = "李四";
	private String sex = "女";
	public synchronized void set(String name,String sex)
	{
		this.name = name ;
		this.sex =sex ;
	}
	public synchronized void get()
	{
		System.out.println(this.name + " ---->" + this.sex ) ;
	}
}

class Consumer implements Runnable {
	P q = null;

	public Consumer(P q) {
		this.q = q;
	}

	public void run() {
		while (true) {
			q.get();
		}
	}
}

public class ThreadCommunation2 {
	public static void main(String[] args) {
		P q = new P();
		new Thread(new Producer(q)).start();
		new Thread(new Consumer(q)).start();
	}
}